<?php
/*  user data (from tables userData and passenger)
    ============================================================================================================== */
	function getUserData( $userId ){
		$sql = "SELECT * FROM userData WHERE id = '" . $userId . "'";
		$res = mysql_query( $sql );
		$userData = mysql_fetch_assoc( $res );
		
		$sql = "SELECT * FROM contactData WHERE id = '" . $userData['contactData_id'] . "'";
		$res = mysql_query( $sql );
		$userDataDetails = mysql_fetch_assoc( $res );
		
		return array_merge( $userData, $userDataDetails );
	}
	

/*  get location-data by id (returns array with all data)
    ============================================================================================================== */
	function getLocation( $id ){
		$sql = "SELECT * FROM productCategory WHERE id = '" . $id . "'";
		$res = mysql_query( $sql );
		$row = mysql_fetch_assoc( $res );
		return( $row );
	}



/*  get product-data by id (returns array with all data)
    ============================================================================================================== */
	function getProduct( $id ){
		$sql = "SELECT * FROM product WHERE id = '" . $id . "'";
		$res = mysql_query( $sql );
		$row = mysql_fetch_assoc( $res );
		return( $row );
	}



/*  get amount of product in section (returns integer (=amount))
    ============================================================================================================== */
	function amountOfProductsInLocation( $locationId ){
		$sql = "SELECT id FROM product WHERE productCategory_id = '" . $locationId . "'";
		$res = mysql_query( $sql );
		return( mysql_num_rows( $res ) );
	}



/*  get the shopping list ID of the user
    ============================================================================================================== */
	function getShoppingListId( $userId ){
		$sql = "SELECT id FROM shoppingList WHERE userData_id = '" . $userId . "'";
		$res = mysql_query( $sql );
		$row = mysql_fetch_array( $res );
		return( $row['id'] );
	}



/*  is the product on the current users shopping list? returns boolean
    ============================================================================================================== */
	function productOnShoppingList( $userId, $productId ){
		$sql = "SELECT id FROM shoppingListItems WHERE shoppingList_id = " . getShoppingListId( $userId ) . " AND product_id = '" . $productId . "'";
		$res = mysql_query( $sql );
		if( mysql_num_rows( $res ) == 0 ){
			return false;
		} else {
			return true;
		}
	}



/*  get the boolean if product is seen in store or not (returns boolean)
    ============================================================================================================== */
	function getShoppingListItemSeenInStore( $productId, $shoppingListId ){
		$sql = "SELECT * FROM shoppingListItems WHERE shoppingList_id = " . $shoppingListId . " AND product_id = '" . $productId . "'";
		$res = mysql_query( $sql );
		$row = mysql_fetch_array( $res );
		if( $row['seenInStore'] == 0 ){
			return false;
		} else {
			return true;
		}
	}



/*  returns the name of the category depending on a given ID
    ============================================================================================================== */
	function getCategoryName( $categoryId ){
		$sql = "SELECT * FROM productCategory WHERE id = " . $categoryId;
		$res = mysql_query( $sql );
		$row = mysql_fetch_array( $res );
		return $row['description'];
	}



/*  returns the color of the category depending on a given ID
    ============================================================================================================== */
	function getCategoryColor( $categoryId ){
		$sql = "SELECT * FROM productCategory WHERE id = " . $categoryId;
		$res = mysql_query( $sql );
		$row = mysql_fetch_array( $res );
		return $row['colorOnMap'];
	}



/*  returns the color of the category depending on a given ID
    ============================================================================================================== */
	function getReviews( $productName = "" ){
		$rss = fetch_rss( 'http://pipes.yahoo.com/pipes/pipe.run?_id=FPQOn_ie3RGdC0I4BR50VA&_render=rss&query=' . $productName );
		return $rss;
	}



/*  get shelf-data by id (returns array with all data)
    ============================================================================================================== */
	function getShelf( $id ){
		$sql = "SELECT * FROM shelfs WHERE shelf_ID = '" . $id . "'";
		$res = mysql_query( $sql );
		if(!$res)echo mysql_error();
		$row = mysql_fetch_array( $res );
		return( $row );
	}



/*  get next product in current shopping-list
    ============================================================================================================== */
	function getNextProductForRouting( $currentId ){
		$nextProductId = "";
		$sql = "SELECT    product.id AS product_id
		        FROM      shoppingListItems 
		        JOIN      product 
		        ON        shoppingListItems.Product_id = product.id 
		        JOIN      shelfs 
		        ON        product.tbl_shelfs_id = shelfs.shelf_ID 
		        WHERE     shoppingListItems.shoppingList_id = " . getShoppingListId( $_SESSION['user_id'] ) . " 
		        AND       shoppingListItems.seenInStore = 0
		        ORDER BY  shelfs.shelf_floor, shelfs.shelf_corridor, product.slotInShelf";
		$res = mysql_query( $sql );
		while( $row = mysql_fetch_array( $res ) ){
			$productList[] = $row['product_id'];
		}
		@reset( $productList );
		if( count( $productList ) > 1 ){
			while ( list( $key, $val ) = each( $productList ) ) {
				if( $val == $currentId ){
					@$nextProductId = $productList[$key + 1];
				}
			}
			if( $nextProductId == "" ){
				return "0";
			} else {
				return $nextProductId;
			}
		} else {
			return "0";
		}
	}



?>